var questionService = require('../../services/backoffice/backOffice-questionService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');
var moment = require('moment');


exports.backPostQuestion = function (req, res) {

    var input = req.body;

    input.community = input.community.split(',');
    if(input.tag.length >0 &&input.tag.indexOf(',')>=0)
    {
        input.tag = input.tag.split(',');
    }else if(input.tag == ''){
        input.tag = null;
    };;

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('postQuestionNew - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
        } else {
            var tempImages = new Array();
            var k = 0;
            for (var i = 0; i < images.length; i++) {
                if (images[i] != null)
                    tempImages[k++] = {
                        "imageURL": images[i]
                    }
            }
            input['images'] = tempImages;

            if (input.deadline != null) {
                input.deadline = moment(Number(input.deadline)).add(-8, 'hour').toDate();
            }

            questionService.backPostQuestionService(input, function (err, result) {
                if (err) {
                    logger.error('backPostQuestion--' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};

//小号发帖
exports.MultipleAccountPostQuestion = function (req, res) {

    var input = req.body;

    input.sendNumbers = input.numbers.split(',');
    if(input.tag.length >0 &&input.tag.indexOf(',')>=0)
    {
        input.tag = input.tag.split(',');
    }else if(input.tag == ''){
        input.tag = null;
    };
    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('postQuestionNew - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
        } else {
            var tempImages = new Array();
            var k = 0;
            for (var i = 0; i < images.length; i++) {
                if (images[i] != null)
                    tempImages[k++] = {
                        "imageURL": images[i]
                    }
            }
            input['images'] = tempImages;

            if (input.deadline != null) {
                input.deadline = moment(Number(input.deadline)).add(-8, 'hour').toDate();
            }

            questionService.MultipleAccountPostQuestionService(input, function (err, result) {
                if (err) {
                    logger.error('MultipleAccountPostQuestion--' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};

//保存贴库帖子
exports.saveQuestionStore = function (req, res) {

    var input = req.body;

    if(input.tag.length >0 &&input.tag.indexOf(',')>=0)
    {
        input.tag = input.tag.split(',');
    }else if(input.tag == ''){
        input.tag = null;
    };

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            logger.error('postQuestionNew - uploadImage - ' + err.message);
            res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
        } else {
            var tempImages = new Array();
            var k = 0;
            for (var i = 0; i < images.length; i++) {
                if (images[i] != null)
                    tempImages[k++] = {
                        "imageURL": images[i]
                    }
            }
            input['images'] = tempImages;

            questionService.saveQuestionStoreService(input, function (err, result) {
                if (err) {
                    logger.error('backPostQuestion--' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, '发起问题失败'));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};


exports.getQuestionStore = function (req, res) {
    var input = req.body;

    var page = req.params.page;
    var size = req.params.size;

    questionService.getQuestionStoreService(page,size,input, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to getQuestionStore ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to getQuestionStore...!'));
        } else {
            var data = {
                totalPage: results[0],
                questionStoreList: results[1]
            };

            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.deleteQuestionStore = function (req, res) {

    var id = req.params.objId;
    questionService.deleteQuestionStoreService(id, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to delete one question...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to delete one question...!'));
        } else {
            if (results != null) {
                logger.debug("Delete one question  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req, null, null));
            } else {
                logger.error('Failed to delete one question, it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req, 'Failed to delete one question , it is not existing! -' + id));
            }
        }
    })
};

exports.updateQuestionStore = function (req, res) {
    var questionId = req.params.objId;
    var input = req.body;

    logger.debug(input);
    questionService.updateQuestionStoreService(questionId, input, function (err, result) {
        if (err) {
            logger.error('Exception happened when trying to update the question data...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to update the question data...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    });
};

exports.createQuestionOrActivity = function (req, res) {
    var input = req.body;

    //logger.debug(input);
    questionService.createQuestionOrActivitService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when trying to add the createQuestionOrActivit data...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to add the createQuestionOrActivit data...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    });
};

exports.uploadImage = function (req, res) {

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, images));
        };
    });
};

exports.getCardByFilter = function (req, res) {
    var input = req.body;

    questionService.getCardByFilterService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when trying to getCard data...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to getCard data...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    });
};

exports.saveCard = function (req, res) {
    var input = req.body;

    questionService.saveCardService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when trying to save the card data...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to save the card data...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        };
    });
};

exports.deleteCard = function (req, res) {

    var id = req.params.objId;
    questionService.deleteCardService(id, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to delete one Card...!');
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when trying to delete one Card...!'));
        } else {
            if (results != null) {
                logger.debug("Delete one Card  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req, null, null));
            } else {
                logger.error('Failed to delete one Card, it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req, 'Failed to delete one Card , it is not existing! -' + id));
            }
        }
    })
};

exports.setCardOrder = function (req, res) {
    var input = req.body.cardId;
    questionService.setCardOrderService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when setCardOrder ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    });

};